package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGameServerResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatGameServer;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.ui.view.MaxHeightRecyclerView;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class RedpackageChooseFragment extends BaseHttpFragment implements View.OnClickListener {


    private MaxHeightRecyclerView mListview;
    private TextView cancelText;
    OnCommStringChoiceListener mListener;

    public static final RedpackageChooseFragment newInstance(Bundle bundle) {
        RedpackageChooseFragment fragment = new RedpackageChooseFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public interface OnCommStringChoiceListener {
        void onCommonStringChoice(String string);
    }

    public void setmListener(OnCommStringChoiceListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void onStart() {
        super.onStart();
//        setEnterAnimationFromBottomStyle(0,true);
    }

//    protected void setEnterAnimationFromBottomStyle(int offset, boolean translucent) {
//        Window window = getwgetDialog().getWindow();
//        if(window != null){
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//
//            window.setWindowAnimations(R.style.AppMessageDialogTheme);
//
//            DisplayMetrics dm = new DisplayMetrics();
//            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//
//            WindowManager.LayoutParams params = window.getAttributes();
//            params.gravity = Gravity.BOTTOM;
//            params.y = offset;
//            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//            if(!translucent){
//                params.dimAmount = 0;
//            }
//            window.setAttributes(params);
//        }
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_redpackage_choose");
        View contentView =inflater.inflate(resource,null);
        cancelText = contentView.findViewById(getViewIdentifier("redpackage_choose_cancel"));
        cancelText.setOnClickListener(this);
        mListview = contentView.findViewById(getViewIdentifier("float_notice_list"));
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                MyFragmentManger.getInstance().gotoFragment(RedpackageOpenFragment.newInstance(null));
                if(mListener!=null)mListener.onCommonStringChoice(myNoticeList.get(i));

                MyFragmentManger.getInstance().goBack(RedpackageChooseFragment.this,true);
            }
        });
        myNoticeList = getArguments().getStringArrayList("list");
        if(getArguments().containsKey("color")){
            colorString = getArguments().getString("color");
        }
        setAdapter();
        return contentView;

    }

    private List<String> myNoticeList;
    private String colorString = "#505050";


    private void setAdapter(){
        mListview.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return myNoticeList==null?0:myNoticeList.size();
            }

            @Override
            public Object getItem(int i) {
                return myNoticeList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ViewHolder holder = null;
                if (view == null) {
                    int resource = ResUtils.getLayoutIdentifier(getContext(), "fb_redpackage_choose_item");
                    view = LayoutInflater.from(getContext()).inflate(resource, null);
                    holder = new ViewHolder();
                    holder.titleText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "redpackage_choose_text"));
                    view.setTag(holder);
                } else {
                    holder = (ViewHolder) view.getTag();
                }
                holder.titleText.setText(myNoticeList.get(i));
                holder.titleText.setTextColor(Color.parseColor(colorString));
                return view;
            }
        });
    }

    class ViewHolder {
        TextView titleText;

    }


    @Override
    public void onClick(View view) {
        if(view ==cancelText){
            MyFragmentManger.getInstance().goBack(this,true);
        }

    }

}
